<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		
	<span class="different-posts different-posts_earlier">📖 <a href="/page/6">earlier posts</a> 📖</span>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/9. shell 练习-日志汇总/">shell 练习-日志汇总</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>我有一个日志（php的slow_log) 几乎每分钟都有输出信息。需要我们来写个脚本分析一下它，目的就是为了归类汇总，按照它们出现的频次做个排序，假如日志是每天0点5分清空，那么按照每小时一次汇总分析该日志，最后在第二天0点0分时，再汇总一下整天的日志，怎么写呢？ 下面我给出一个日志样例和我写的分析汇总脚本供参考。日志样例：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div></pre></td><td class="code"><pre><div class="line">[24-Oct-2013 00:05:39]  [pool www.lishiming.net] pid 19101</div><div class="line">script_filename = /data/release/www.lishiming.net/forum.php</div><div class="line">[0x00007f9279237e98] mysql_unbuffered_query() /data/release/www.lishiming.net/<span class="built_in">source</span>/class/db/db_driver_mysql.php:147</div><div class="line">[0x00007f92792377a8] query() /data/release/www.lishiming.net/<span class="built_in">source</span>/class/discuz/discuz_database.php:136</div><div class="line">[0x00007f9279236f40] query() /data/release/www.lishiming.net/<span class="built_in">source</span>/class/table/table_forum_thread.php:932</div><div class="line">[0x00007f92792365e8] increase() /data/release/www.lishiming.net/<span class="built_in">source</span>/module/forum/forum_viewthread.php:1034</div><div class="line">[0x00007f9279218f48] viewthread_updateviews() /data/release/www.lishiming.net/<span class="built_in">source</span>/module/forum/forum_viewthread.php:353</div><div class="line">[0x00007f9279218050] +++ dump failed</div><div class="line"></div><div class="line">[24-Oct-2013 00:05:39]  [pool www.lishiming.net] pid 19754</div><div class="line">script_filename = /data/release/www.lishiming.net/forum.php</div><div class="line">[0x00007f9279237938] mysql_query() /data/release/www.lishiming.net/<span class="built_in">source</span>/class/db/db_driver_mysql.php:147</div><div class="line">[0x00007f9279237248] query() /data/release/www.lishiming.net/<span class="built_in">source</span>/class/discuz/discuz_database.php:136</div><div class="line">[0x00007f9279236dc0] query() /data/release/www.lishiming.net/<span class="built_in">source</span>/class/discuz/discuz_database.php:100</div><div class="line">[0x00007f9279235d48] fetch_all() /data/release/www.lishiming.net/<span class="built_in">source</span>/class/table/table_forum_thread.php:523</div><div class="line">[0x00007f9279218f48] fetch_all_search() /data/release/www.lishiming.net/<span class="built_in">source</span>/module/forum/forum_forumdisplay.php:637</div><div class="line">[0x00007f9279218050] +++ dump failed</div><div class="line"></div><div class="line">[24-Oct-2013 00:06:07]  [pool www.lishiming.net] pid 22624</div><div class="line">script_filename = /data/release/www.lishiming.net/forum.php</div><div class="line">[0x00007f9279237938] mysql_query() /data/release/www.lishiming.net/<span class="built_in">source</span>/class/db/db_driver_mysql.php:147</div><div class="line">[0x00007f9279237248] query() /data/release/www.lishiming.net/<span class="built_in">source</span>/class/discuz/discuz_database.php:136</div><div class="line">[0x00007f9279236dc0] query() /data/release/www.lishiming.net/<span class="built_in">source</span>/class/discuz/discuz_database.php:100</div><div class="line">[0x00007f9279235d48] fetch_all() /data/release/www.lishiming.net/<span class="built_in">source</span>/class/table/table_forum_thread.php:523</div><div class="line">[0x00007f9279218f48] fetch_all_search() /data/release/www.lishiming.net/<span class="built_in">source</span>/module/forum/forum_forumdisplay.php:637</div><div class="line">[0x00007f9279218050] +++ dump failed</div><div class="line"></div><div class="line">[24-Oct-2013 00:06:18]  [pool www.lishiming.net] pid 22624</div><div class="line">script_filename = /data/release/www.lishiming.net/forum.php</div><div class="line">[0x00007f9279237938] mysql_query() /data/release/www.lishiming.net/<span class="built_in">source</span>/class/db/db_driver_mysql.php:147</div><div class="line">[0x00007f9279237248] query() /data/release/www.lishiming.net/<span class="built_in">source</span>/class/discuz/discuz_database.php:136</div><div class="line">[0x00007f9279236dc0] query() /data/release/www.lishiming.net/<span class="built_in">source</span>/class/discuz/discuz_database.php:100</div><div class="line">[0x00007f9279235d48] fetch_all() /data/release/www.lishiming.net/<span class="built_in">source</span>/class/table/table_forum_thread.php:523</div><div class="line">[0x00007f9279218f48] fetch_all_search() /data/release/www.lishiming.net/<span class="built_in">source</span>/module/forum/forum_forumdisplay.php:637</div><div class="line">[0x00007f9279218050] +++ dump failed</div></pre></td></tr></table></figure>
<p>脚本：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#! /bin/bash </span></div><div class="line"></div><div class="line">slow_log=/usr/<span class="built_in">local</span>/php/<span class="built_in">log</span>/php.slow</div><div class="line"></div><div class="line">d=`date -d <span class="string">"-1 minute"</span> +%H:%M`</div><div class="line"></div><div class="line">d_h=`date +%H`</div><div class="line"></div><div class="line">d_m=`date +%M`</div><div class="line"></div><div class="line">d_d=`date +%Y%d`</div><div class="line"></div><div class="line">d_d2=`date -d <span class="string">"-1 day"</span> +%Y%d`</div><div class="line"></div><div class="line">logdir=<span class="string">"/log/php_slow/<span class="variable">$d_d</span>"</span></div><div class="line"></div><div class="line">logdir2=<span class="string">"/log/php_slow/<span class="variable">$d_d2</span>"</span></div><div class="line"></div><div class="line">[ -d <span class="variable">$logdir</span> ] || mkdir -p <span class="variable">$logdir</span></div><div class="line"></div><div class="line">[ -d <span class="variable">$logdir2</span> ] || mkdir -p <span class="variable">$logdir2</span></div><div class="line"></div><div class="line"><span class="keyword">if</span> [ <span class="variable">$d_m</span> -eq <span class="string">"00"</span> ]; <span class="keyword">then</span> </div><div class="line"></div><div class="line">    d1=`date -d <span class="string">"-1 hour"</span> +%H`</div><div class="line"></div><div class="line">    n1=`grep -n <span class="string">" <span class="variable">$d1</span>:[0-9][0-9]:"</span> <span class="variable">$slow_log</span>|head -n1 |awk -F<span class="string">':'</span> <span class="string">'&#123;print $1&#125;'</span>`</div><div class="line"></div><div class="line">    n2=`wc -l <span class="variable">$slow_log</span> |awk <span class="string">'&#123;print $1&#125;'</span>`</div><div class="line"></div><div class="line">    n3=$[<span class="variable">$n2</span>-<span class="variable">$n1</span>]</div><div class="line"></div><div class="line">    tail -n <span class="variable">$n3</span> <span class="variable">$slow_log</span>&gt;/tmp/1.txt</div><div class="line"></div><div class="line">    sed <span class="string">'s/\[0x.*\]//g'</span> /tmp/1.txt |xargs &gt; /tmp/2.txt</div><div class="line"></div><div class="line">    n=`grep <span class="string">'\[pool'</span> /tmp/1.txt|wc -l`</div><div class="line"></div><div class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 <span class="variable">$n</span>`; <span class="keyword">do</span> awk -F <span class="string">'+++ dump failed'</span> <span class="string">'&#123;print $'</span><span class="string">"<span class="variable">$i</span>"</span><span class="string">'&#125;'</span> /tmp/2.txt; <span class="keyword">done</span> &gt; /tmp/3.txt</div><div class="line"></div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$d_h</span> -ne <span class="string">"00"</span> ]; <span class="keyword">then</span></div><div class="line"></div><div class="line">        sed <span class="string">'s/^.*script_filename = //'</span> /tmp/3.txt |grep -v <span class="string">'^$'</span>|sort |uniq -c |sort -rn &gt; <span class="variable">$logdir</span>/<span class="variable">$d1</span>\_slow_log</div><div class="line"></div><div class="line">    <span class="keyword">else</span></div><div class="line"></div><div class="line">        sed <span class="string">'s/^.*script_filename = //'</span> /tmp/3.txt |grep -v <span class="string">'^$'</span>|sort |uniq -c |sort -rn &gt; <span class="variable">$logdir2</span>/<span class="variable">$d1</span>\_slow_log</div><div class="line"></div><div class="line">        sed <span class="string">'s/^.*[0-9] \//\//'</span> <span class="variable">$logdir2</span>/*_log |sort |uniq -c |sort -rn &gt; <span class="variable">$logdir2</span>/<span class="variable">$d_d2</span>\_slow_log</div><div class="line"></div><div class="line">    <span class="keyword">fi</span></div><div class="line"></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/8. shell 练习/">shell 练习8</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>需求： 两台web:A和B机器，其中一台B只打算跑附件（大多为图片），另一台A跑除了附件外的其他东西。其中附件会在A上生成，默认附件会通过A机器访问，但当我们更改某个附件的标志位时，那么附件则会跑到B上去访问。 假如提供给我们了将要在B上跑的附件列表和更改这些附件的标志位的一个sql文件，那么我们如何做呢？</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#! /bin/bash</span></div><div class="line"><span class="comment">## 1. rsync att to remote server.</span></div><div class="line"><span class="comment">## 2. execute the sql for changing tag 0 to 1.</span></div><div class="line"><span class="comment">## 3. delete the file att_list and sql.</span></div><div class="line">base_dir=/data/wwwroot/bbs.xxx.com/data</div><div class="line">r_dir=160.11.274.32::img</div><div class="line">d=`date +%Y%m%d`</div><div class="line"><span class="comment">#文件列表的文件</span></div><div class="line">f_list=<span class="variable">$base_dir</span>/attachment_move_list.php   </div><div class="line"><span class="comment">#更改附件标志位的sql文件</span></div><div class="line">f_sql=<span class="variable">$base_dir</span>/attachment_move_sql.php  更改附件标志位的sql文件</div><div class="line">file_dir=<span class="variable">$base_dir</span>/oldatt_list_sql</div><div class="line"><span class="keyword">if</span> [ ! -d <span class="variable">$file_dir</span> ] ; <span class="keyword">then</span></div><div class="line">    mkdir <span class="variable">$file_dir</span></div><div class="line"><span class="keyword">fi</span></div><div class="line"><span class="keyword">if</span> [ ! -f <span class="variable">$f_list</span> ] || [ ! -f <span class="variable">$f_sql</span> ]; <span class="keyword">then</span></div><div class="line">    <span class="built_in">exit</span></div><div class="line"><span class="keyword">fi</span></div><div class="line">grep -v <span class="string">'exit()'</span> <span class="variable">$f_list</span> |sed <span class="string">'s#/data/wwwroot/bbs.xxx.com/data/attachment/forum#.#'</span>  &gt; /tmp/1.txt</div><div class="line"><span class="built_in">cd</span> <span class="variable">$base_dir</span>/attachment/forum</div><div class="line">rsync -aL --files-from=/tmp/1.txt ./ <span class="variable">$r_dir</span>/</div><div class="line">/bin/mv <span class="variable">$f_list</span> <span class="variable">$file_dir</span>/<span class="variable">$d</span>.list</div><div class="line">grep -v <span class="string">'exit()'</span> <span class="variable">$f_sql</span> &lt; /tmp/2.txt</div><div class="line">mysql -h192.168.1.11 -uuser -p<span class="string">'passwd'</span> bbs &amp;lt; /tmp/2.txt</div><div class="line">/bin/mv <span class="variable">$f_sql</span> <span class="variable">$file_dir</span>/<span class="variable">$d</span>.sql</div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/7. shell 练习-备份数据库/">shell 练习-备份数据库</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>设计一个shell脚本来备份数据库，首先在本地服务器上保存一份数据，然后再远程拷贝一份，本地保存一周的数据，远程保存一个月。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;假定，我们知道mysql root账号的密码，要备份的库为discuz，本地备份目录为/bak/mysql, 远程服务器ip为192.168.123.30，远程提供了一个rsync服务，备份的地址是 192.168.123.30::backup  . 写完脚本后，需要加入到cron中，每天凌晨3点执行。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">##backup mysql</span></div><div class="line"><span class="comment">##writen by yanyi</span></div><div class="line"></div><div class="line">PATH=<span class="variable">$PATH</span>:/usr/<span class="built_in">local</span>/mysql/bin</div><div class="line">bakdir=/bak/mysql</div><div class="line">r_bakdir=192.168.123.30:backup</div><div class="line">d1=`date +%w`</div><div class="line">d2=`date +%d`</div><div class="line">passwd=<span class="string">"mysql_password"</span></div><div class="line"></div><div class="line"><span class="built_in">exec</span> 1&gt; /var/<span class="built_in">log</span>/mysqlbak.log 2&gt;/var/<span class="built_in">log</span>/mysqlbak.log</div><div class="line"><span class="built_in">echo</span> <span class="string">"mysql backup begin at `date +"</span>%F %T<span class="string">"`."</span></div><div class="line">mysqldump -uroot -p<span class="variable">$passwd</span> --default-character=gbk discuz &gt; <span class="variable">$d1</span>.sql</div><div class="line">rsync -a <span class="variable">$bakdir</span>/<span class="variable">$d1</span>.sql <span class="variable">$r_bakdir</span>/<span class="variable">$d2</span>.sql</div><div class="line"><span class="built_in">echo</span> <span class="string">"mysql backup end at `date +"</span>%F %T<span class="string">"`."</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后加入cron</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">0 3 * * *  /bin/bash  /usr/<span class="built_in">local</span>/sbin/mysqlbak.sh</div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/65.shell 练习-成员分组/">shell 练习-成员分组</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>需求是，把所有的成员平均得分成若干个小组。这里，我会提供一个人员列表，比如成员有50人，需要分成7个小组，要求随机性，每次和每次分组的结构应该不一致。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#假设成员列表文件为members.txt</span></div><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">f=members.txt</div><div class="line">n=`wc -l <span class="variable">$f</span>|awk <span class="string">'&#123;print $1&#125;'</span>`</div><div class="line"></div><div class="line">get_n()</div><div class="line">&#123;</div><div class="line">    l=`<span class="built_in">echo</span> <span class="variable">$1</span>|wc -c`</div><div class="line">    n1=<span class="variable">$RANDOM</span></div><div class="line">    n2=$[<span class="variable">$n1</span>+<span class="variable">$l</span>]</div><div class="line">    g_id=$[<span class="variable">$n1</span>%7]</div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$g_id</span> -eq 0 ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">	g_id=7</div><div class="line">    <span class="keyword">fi</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$g_id</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 7`</div><div class="line"><span class="keyword">do</span></div><div class="line">    [ -f n_<span class="variable">$i</span>.txt ] &amp;&amp; rm -f n_<span class="variable">$i</span>.txt</div><div class="line"><span class="keyword">done</span></div><div class="line"></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 <span class="variable">$n</span>`</div><div class="line"><span class="keyword">do</span></div><div class="line">    name=`sed -n <span class="string">"<span class="variable">$i</span>"</span>p <span class="variable">$f</span>`</div><div class="line">    g=`get_n <span class="variable">$name</span>`</div><div class="line">    <span class="built_in">echo</span> <span class="variable">$name</span> &gt;&gt; n_<span class="variable">$g</span>.txt</div><div class="line"><span class="keyword">done</span></div><div class="line"></div><div class="line"><span class="function"><span class="title">nu</span></span>()&#123;</div><div class="line">    wc -l <span class="variable">$1</span>|awk <span class="string">'&#123;print $1&#125;'</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">max</span></span>()&#123;</div><div class="line">    ma=0</div><div class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 7`</div><div class="line">    <span class="keyword">do</span></div><div class="line">	n=`nu n_<span class="variable">$i</span>.txt`</div><div class="line">	<span class="keyword">if</span> [ <span class="variable">$n</span> -gt <span class="variable">$ma</span> ]</div><div class="line">	<span class="keyword">then</span></div><div class="line">	    ma=<span class="variable">$n</span></div><div class="line">	<span class="keyword">fi</span></div><div class="line">    <span class="keyword">done</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$ma</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">min</span></span>()&#123;</div><div class="line">    mi=50</div><div class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 7`</div><div class="line">    <span class="keyword">do</span></div><div class="line">	n=`nu n_<span class="variable">$i</span>.txt`</div><div class="line">	<span class="keyword">if</span> [ <span class="variable">$n</span> -lt <span class="variable">$mi</span> ]</div><div class="line">	<span class="keyword">then</span></div><div class="line">	    mi=<span class="variable">$n</span></div><div class="line">	<span class="keyword">fi</span></div><div class="line">    <span class="keyword">done</span></div><div class="line">    <span class="built_in">echo</span> <span class="variable">$mi</span></div><div class="line">&#125;</div><div class="line"></div><div class="line">ini_min=1</div><div class="line"></div><div class="line"><span class="keyword">while</span> [ <span class="variable">$ini_min</span> -le 7 ]</div><div class="line"><span class="keyword">do</span></div><div class="line">    m1=`max`</div><div class="line">    m2=`min`</div><div class="line">    ini_min=m2</div><div class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 7`</div><div class="line">    <span class="keyword">do</span></div><div class="line">	n=`nu n_<span class="variable">$i</span>.txt`</div><div class="line">	<span class="keyword">if</span> [ <span class="variable">$n</span> -eq <span class="variable">$m1</span> ]</div><div class="line">	<span class="keyword">then</span></div><div class="line">	    f1=n_<span class="variable">$i</span>.txt</div><div class="line">	<span class="keyword">elif</span> [ <span class="variable">$n</span> -eq <span class="variable">$m2</span> ]</div><div class="line">	<span class="keyword">then</span></div><div class="line">	    f2=n_<span class="variable">$i</span>.txt</div><div class="line">	<span class="keyword">fi</span></div><div class="line">    <span class="keyword">done</span></div><div class="line">    name=`tail -n1 <span class="variable">$f1</span>`</div><div class="line">    <span class="built_in">echo</span> <span class="variable">$name</span> &gt;&gt; <span class="variable">$f2</span></div><div class="line">    sed -i <span class="string">"/<span class="variable">$name</span>/d"</span> <span class="variable">$f1</span></div><div class="line">    ini_min=$[<span class="variable">$ini_min</span>+1]</div><div class="line"><span class="keyword">done</span></div><div class="line"></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 7`</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"<span class="variable">$i</span> 组成员有："</span></div><div class="line">    cat n_<span class="variable">$i</span>.txt</div><div class="line">    <span class="built_in">echo</span></div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/64.shell 练习-计算重复单词个数/">shell 练习-计算重复单词个数</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>将文件内所有的单词的重复次数计算出来，只需要列出重复次数最多的10个单词。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#假设文档名字叫做a.txt</span></div><div class="line">sed <span class="string">'s/[^a-zA-Z]/ /g'</span> a.txt|xargs -n1 |sort |uniq -c |sort -nr |head</div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/63.shell 练习-备份etc目录/">shell 练习-备份/etc</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>设计一个shell程序，在每月第一天备份并压缩/etc目录的所有内容，存放在/root/bak目录里，且文件名为如下形式”yymmdd_etc.tar.gz”，yy为年，mm为月，dd为日。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/sh </span></div><div class="line"><span class="keyword">if</span> [ -d /root/bak ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    mkdir /root/bak</div><div class="line"><span class="keyword">fi</span></div><div class="line">prefix=`date +%y%m%d`</div><div class="line">d=`date +%m`</div><div class="line"><span class="keyword">if</span> [ <span class="variable">$d</span> == <span class="string">"01"</span> ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">cd</span> /etc/</div><div class="line">    tar czf  /root/bak/<span class="variable">$prefix_etc</span>.tar.gz ./</div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/62.shell 练习-给文档增加内容/">shell 练习-给文档增加内容</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>在文本文档1.txt第5行后面增加如下内容：    </p>
<h1 id="This-is-a-test-file"><a href="#This-is-a-test-file" class="headerlink" title="This is a test file."></a>This is a test file.</h1><h1 id="Test-insert-line-into-this-file"><a href="#Test-insert-line-into-this-file" class="headerlink" title="Test insert line into this file."></a>Test insert line into this file.</h1><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">sed -i <span class="string">"5a # This is a test file.\n# Test insert line into this file."</span> 1.txt</div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/61.shell 练习-打印数字/">shell 练习-打印数字</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>写一个shell脚本。提示你输入一个暂停的数字，然后从1打印到该数字。然后询问是否继续。继续的话在输入个在数字 接着打印。不继续退出。</p>
<p>例：如果输入的是5，打印1 2 3 4 5  然后继续 输入15   然后打印 6 7 …14 15 依此类推。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="built_in">read</span> -p <span class="string">"请输入您想要暂停的数字："</span> number_1</div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `seq 1 <span class="variable">$number_1</span>`;</div><div class="line"><span class="keyword">do</span></div><div class="line">        <span class="built_in">echo</span> <span class="variable">$i</span></div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="built_in">read</span> -p <span class="string">"是否继续输入数字？"</span> a</div><div class="line"><span class="keyword">if</span> [ <span class="variable">$a</span> == <span class="string">"yes"</span> ];<span class="keyword">then</span></div><div class="line">        <span class="built_in">read</span> -p <span class="string">"请继续输入您想要暂停的数字："</span> number_2</div><div class="line">        number_3=$[<span class="variable">$number_1</span>+1]</div><div class="line">        <span class="keyword">if</span> [ <span class="variable">$number_2</span> -gt <span class="variable">$number_1</span> ];<span class="keyword">then</span></div><div class="line">                <span class="keyword">for</span> h <span class="keyword">in</span> `seq <span class="variable">$number_3</span> <span class="variable">$number_2</span>`;</div><div class="line">                <span class="keyword">do</span></div><div class="line">                        <span class="built_in">echo</span> <span class="variable">$h</span></div><div class="line">                <span class="keyword">done</span></div><div class="line">        <span class="keyword">else</span></div><div class="line">                <span class="built_in">echo</span> <span class="string">"输入数字错误，请输入大于的数字!"</span></div><div class="line">        <span class="keyword">fi</span></div><div class="line"><span class="keyword">else</span></div><div class="line">        <span class="built_in">exit</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/60.shell 练习-统计分析日志/">shell 练习-统计分析日志</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>已知nginx访问的日志文件在/usr/local/nginx/logs/access.log内</p>
<p>请统计下早上10点到12点 来访ip最多的是哪个?</p>
<p>日志样例：</p>
<blockquote>
<p>111.199.186.68 - [15/Sep/2017:09:58:37 +0800]  “//plugin.php?id=security:job” 200 “POST //plugin.php?id=security:job HTTP/1.1””<a href="http://a.lishiming.net/forum.php?mod=viewthread&amp;tid=11338&amp;extra=page%3D1%26filter%3Dauthor%26orderby%3Ddateline" target="_blank" rel="external">http://a.lishiming.net/forum.php?mod=viewthread&amp;tid=11338&amp;extra=page%3D1%26filter%3Dauthor%26orderby%3Ddateline</a>“ “Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3141.7 Safari/537.36” “0.516”<br>203.208.60.208 - [15/Sep/2017:09:58:46 +0800] “/misc.php?mod=patch&amp;action=ipnotice&amp;_r=0.05560809863330207&amp;inajax=1&amp;ajaxtarget=ip_notice” 200 “GET /misc.php?mod=patch&amp;action=ipnotice&amp;_r=0.05560809863330207&amp;inajax=1&amp;ajaxtarget=ip_notice HTTP/1.1””<a href="http://a.lishiming.net/forum.php?mod=forumdisplay&amp;fid=65&amp;filter=author&amp;orderby=dateline" target="_blank" rel="external">http://a.lishiming.net/forum.php?mod=forumdisplay&amp;fid=65&amp;filter=author&amp;orderby=dateline</a>“ “Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3141.7 Safari/537.36” “0.065”</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">grep <span class="string">'15/Sep/2017:1[0-2]:[0-5][0-9]:'</span> /usr/<span class="built_in">local</span>/nginx/logs/access.log|awk <span class="string">'&#123;print $1&#125;'</span>|sort -n|uniq -c |tail -n1</div></pre></td></tr></table></figure>
	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Shell 练习/6. shell 练习-判断80端口是否开启/">shell 练习-判断80端口是否开启</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>写一个脚本，判断本机的80端口是否开启着，如果开启着什么都不做，如果发现端口不存在，那么重启一下httpd服务，并发邮件通知你自己。脚本写好后，可以每一分钟执行一次，也可以写一个死循环的脚本，30s检测一次。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">mail=123@123.com</div><div class="line"><span class="keyword">if</span> netstat -lnp|grep <span class="string">':80'</span>|grep -q <span class="string">'LISTEN'</span></div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">exit</span></div><div class="line"><span class="keyword">else</span></div><div class="line">    /usr/<span class="built_in">local</span>/apache2/bin/apachectl restart &gt; /dev/null 2&gt; /dev/null</div><div class="line">    <span class="built_in">echo</span> <span class="string">"The 80 port is down."</span>|mail -s <span class="string">'check_80'</span> <span class="variable">$mail</span></div><div class="line">    n=`pa aux|grep httpd|grep -cv grep`</div><div class="line">    <span class="keyword">if</span> [<span class="variable">$n</span> -eq 0 ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        /usr/<span class="built_in">local</span>/apache2/bin/apachectl start 2&gt; tmp/apache_start.err</div><div class="line">    <span class="keyword">fi</span></div><div class="line">    <span class="keyword">if</span> [ -s /tmp/apache_start.err ]</div><div class="line">    <span class="keyword">then</span></div><div class="line">        mail -s <span class="string">'apache_start_error'</span> <span class="variable">$mail</span> &lt; /tmp/apache_start.err</div><div class="line">    <span class="keyword">fi</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;脚本用 crontab -e 加入任务计划，每分钟执行一次     </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以用 while 死循环</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">mail=123@123.com</div><div class="line"><span class="keyword">while</span> :</div><div class="line"><span class="keyword">do</span></div><div class="line">    <span class="keyword">if</span> netstat -lnp|grep <span class="string">':80'</span>|grep -q <span class="string">'LISTEN'</span></div><div class="line">    <span class="keyword">then</span></div><div class="line">        <span class="built_in">exit</span></div><div class="line">    <span class="keyword">else</span></div><div class="line">        /usr/<span class="built_in">local</span>/apache2/bin/apachectl restart &gt; /dev/null 2&gt; /dev/null</div><div class="line">        <span class="built_in">echo</span> <span class="string">"The 80 port is down."</span>|mail -s <span class="string">'check_80'</span> <span class="variable">$mail</span></div><div class="line">        n=`pa aux|grep httpd|grep -cv grep`</div><div class="line">        <span class="keyword">if</span> [<span class="variable">$n</span> -eq 0 ]</div><div class="line">        <span class="keyword">then</span></div><div class="line">            /usr/<span class="built_in">local</span>/apache2/bin/apachectl start 2&gt; tmp/apache_start.err</div><div class="line">        <span class="keyword">fi</span></div><div class="line">        <span class="keyword">if</span> [ -s /tmp/apache_start.err ]</div><div class="line">        <span class="keyword">then</span></div><div class="line">            mail -s <span class="string">'apache_start_error'</span> <span class="variable">$mail</span> &lt; /tmp/apache_start.err</div><div class="line">        <span class="keyword">fi</span></div><div class="line">    <span class="keyword">fi</span></div><div class="line">    sleep 30</div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>

	

	

</article>





	<span class="different-posts">📖 <a href="/page/8">more posts</a> 📖</span>



	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
